import { Alert, Button, Result } from "antd";
import React from "react";
import history from '../utils/history'
import { MicroAppContext } from "..";
import { debug } from "../utils/log";
export default class ErrorBoundary extends React.Component<any, any> {
    unlisten: (() => void) | undefined;
    static contextType?: React.Context<any> = MicroAppContext;
    constructor(props: any) {
        super(props);
        this.state = {
            hasError: false,
            message: '',
            stack: ''
        };

    }
    componentDidMount(): void {
        this.unlisten = history.listen(() => {
            debug('路由改变')
            this.setState({
                hasError: false,
            })
        })
    }
    componentWillUnmount(): void {
        if (this.unlisten) {
            this.unlisten()
        }
    }

    componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void {
        debug({ error })
        this.setState({
            hasError: true,
            message: error.message,
            stack: error.stack
        })
    }
    render() {

        if (this.state.hasError) {
           
            // 你可以自定义降级后的 UI 并渲染
            return (
                <div>页面渲染错误</div>
            );
        }

        return this.props.children;
    }

}